<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
    String id = input.getDesc("id", "-1");
    List<Arrangement> list = QBuilder.db("basedb").select().from("Arrangement").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("id=", id).and("partner=", token.getUid())).query().getResultObj();
    if (isEmpty(list)) {
        msg = new Message(30041, "暂无日程");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
    Arrangement ag = list.get(0);
    Map<String, Object> map = Tools.toMap(ag);
    String team = ag.getTeam();
    boolean self = token.getUid().equals(ag.getUid());   // 是否创建者
    List<Arrangement> ags = QBuilder.db("basedb").select().from("Arrangement").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("team=", team)).query().getResultObj();
    if (isEmpty(ags)) {
        msg = new Message(30041, "暂无日程");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
    List<Integer> ids = new ArrayList<Integer>();
    for (int i = 0; i < ags.size(); i++) {
        ag = ags.get(i);
        Integer partner = ag.getPartner();
        if (!ids.contains(partner))
            ids.add(partner);
    }
    ag = ags.get(0);
    User user = userByUid(ag.getUid());
    if (user == null) {
        msg = new Message(30041, "暂无日程");
        msg.put("data", new HashMap());
        Tools.encryptPrint(out, msg);
        return;
    }
    Map<Integer, User> users = usersByUids(ids);
    map.put("author", user.getName());
    map.put("avatar", user.getAvatar());
    List<Map> doctors = new ArrayList<Map>();
    List<Map> patients = new ArrayList<Map>();
    for (int i = 0; i < ags.size(); i++) {
        Arrangement _ag = ags.get(i);
        String _doctors = _ag.getDoctors();
        String _patients = _ag.getPatients();
        Integer partner = _ag.getPartner();
        user = users.get(partner);
        if (user == null) {
            continue;
        }
        if ("Y".equals(_doctors)) {
            Map doctor = new HashMap();
            doctor.put("name", user.getName());
            doctor.put("avatar", user.getAvatar());
            doctor.put("id", user.getId());
            doctors.add(doctor);
        }
        if ("Y".equals(_patients)) {
            Map patient = new HashMap();
            patient.put("name", user.getName());
            patient.put("avatar", user.getAvatar());
            patient.put("id", user.getId());
            patients.add(patient);
        }
    }
    map.put("doctors", doctors);
    map.put("patients", patients);
    map.put("create", self);
    msg.put("data", map);
    Tools.encryptPrint(out, msg);
%>
